﻿//-------------------------------------------------------------------
//	Copyright (c) 2010-2014  Zhirnov Andrey
//	This file is part of the "UX Standard Lib" project.
//	See copyright notice in "UX_STL_Ext.h".
//-------------------------------------------------------------------

#include "GLViewport.h"

namespace UX_STL
{
namespace GLShell
{
	
/*
=================================================
	
=================================================
*/
	GLViewport::GLViewport(): _bAutoCorrection(true)
	{
		SetViewport( irect(0,0,128,128) );
		_Setup2DMode( frect(0.f,0.f,1.f,1.f) );
	}
	
/*
=================================================
	
=================================================
*/
	GLViewport::~GLViewport()
	{
	}
	
/*
=================================================
	
=================================================
*/
	inline void GLViewport::_CurrectResolution()
	{
		_vResCorrect	= _sViewRect.GetSize() / Size().To<fvec2>();
		_vResCorAspect	= _vResCorrect.x > _vResCorrect.y ?
							fvec2( 1.f, _vResCorrect.y/_vResCorrect.x ) :
							fvec2( _vResCorrect.x/_vResCorrect.y, 1.f );
	}
	
/*
=================================================
	
=================================================
*/
	void GLViewport::_Setup2DMode(const frect &sViewRect, const fvec2 &vClipPlanes)
	{
		_sViewRect			= sViewRect;
		_sOrthoMatrix		= fmat4::BuildOrtho( _sViewRect, vClipPlanes );
		_sFlipOrthoMatrix	= fmat4::BuildOrtho( frect( _sViewRect.left, _sViewRect.top, _sViewRect.right, _sViewRect.bottom ), vClipPlanes );
		
		if ( _bAutoCorrection )	_CurrectResolution();
	}

/*
=================================================
	
=================================================
*/
	void GLViewport::SetResCorrection(const fvec2 &vRC)
	{
		_vResCorrect	= vRC;
		_vResCorAspect	= _vResCorrect.x > _vResCorrect.y ?
							fvec2( 1.f, _vResCorrect.y / _vResCorrect.x ) :
							fvec2( _vResCorrect.x / _vResCorrect.y, 1.f );
	}
	
/*
=================================================
	
=================================================
*/
	void GLViewport::SetAutoCorrection(bool bEnabled)
	{
		if ( _bAutoCorrection = bEnabled )	_CurrectResolution();
	}

//-------------------------------------------------------------------

}	// GLShell
}	// UX_STL